//
//  PKFAQViewController.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/14.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import SnapKit

class PKFAQViewController: PKBaseViewController , NavigationBarConfigurable {
    var shouldHideNavigationBar: Bool { true }
    var showBackButton: Bool { false }
    let backButton = UIButton(type: .custom)
    let titleLabel = UILabel()
    private var tableView : UITableView!
    
    override func setupUI() {
        setupNavigationBar()
        view.backgroundColor = .white
        backButton.setImage(.init(named: "back"), for: .normal)
        view.addSubview(backButton)

        titleLabel.text = "UserReviews"
        titleLabel.font = .adaptedBoldFont(size: 18)
        view.addSubview(titleLabel)
        
        
        tableView = UITableView(frame: .zero, style: .plain)
        view.addSubview(tableView)
        tableView.tableHeaderView = UIView(frame: .init(x: 0, y: 0, width: kWidth, height: 0.01))
        tableView.tableFooterView = UIView(frame: .init(x: 0, y: 0, width: kWidth, height: 0.01))
        tableView.register(UINib(nibName: "PKFAQTableViewCell", bundle: nil), forCellReuseIdentifier: "PKFAQTableViewCell")
        let sections = Observable.just([reviewSection(header: "", items: [.reviewSectionCell(name: "name", description: "description", time: "time"),
                                                                          .reviewSectionCell(name: "name", description: "description", time: "time"),
                                                                          .reviewSectionCell(name: "name", description: "description", time: "time"),
                                                                          .reviewSectionCell(name: "name", description: "description", time: "time"),
                                                                          .reviewSectionCell(name: "name", description: "description", time: "time"),
                                                                          .reviewSectionCell(name: "name", description: "description", time: "time")])])
        let dataSource = RxTableViewSectionedReloadDataSource<reviewSection> { data, table, indexPath, item in
            let cell = table.dequeueReusableCell(withIdentifier: "PKFAQTableViewCell")!
            
            return cell
        }
        
        sections.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag)
 
        
        
    }
    
    override func bindEvents() {
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(DeviceHelper.navigationBarHeight + 20)
        }
        
        
        backButton.snp.makeConstraints { make in
            make.left.equalTo(ScreenAdapter.x(20))
            make.top.equalTo(DeviceHelper.navigationBarHeight + 20)
            make.width.height.equalTo(ScreenAdapter.x(30))
        }
        tableView.snp.makeConstraints { make in
            make.left.equalTo(ScreenAdapter.x(20))
            make.right.equalTo(ScreenAdapter.x(-20))
            make.top.equalTo(DeviceHelper.navigationFullHeight+20)
            make.bottom.equalTo(-DeviceHelper.bottomSafeAreaHeight)
        }
        
        
    }
    
    override func bindViewModel() {
        backButton.rx.tap.subscribe { _ in
            self.navigationController?.popViewController(animated: true)
        }.disposed(by: rx.disposeBag)
        
        tableView.rx.setDelegate(self).disposed(by: rx.disposeBag)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
extension PKFAQViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    
}




private enum ReviewSectionItem {
    case reviewSectionCell(name: String, description: String, time : String)
}

private struct reviewSection {
    var header : String
    var items : [ReviewSectionItem]
}
 extension reviewSection : SectionModelType {
    typealias Item = ReviewSectionItem
    init(original: reviewSection, items: [Item]) {
        self = original
        self.items = items
    }
}
